package com.libinyn.base.pms.controller.sys;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.libinyn.base.pms.common.annotation.SysLog;
import com.libinyn.base.pms.common.utils.Result;
import com.libinyn.base.pms.common.validator.ValidatorUtils;
import com.libinyn.base.pms.controller.AbstractController;
import com.libinyn.base.pms.entity.sys.SysRoleEntity;
import com.libinyn.base.pms.serivce.sys.SysRoleMenuService;
import com.libinyn.base.pms.serivce.sys.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 角色管理
 * @fileName: SysRoleController.java
 * @author: bin.li01
 * @date: 2017/12/12 18:40 
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {

	@Resource
	private SysRoleService sysRoleService;
	@Resource
	private SysRoleMenuService sysRoleMenuService;
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:role:list")
	public Result list(SysRoleEntity sysRoleEntity,PageInfo<SysRoleEntity> pageInfo){
		//如果不是超级管理员，则只查询自己所拥有的角色列表
		sysRoleEntity.setCreateBy(getUserId());
		if (StringUtils.isEmpty(sysRoleEntity.getRoleName())){
			sysRoleEntity.setRoleName(null);
		}
		List<SysRoleEntity> sysRoleEntities;
		try {
			PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
			sysRoleEntities = sysRoleService.select(sysRoleEntity);
			pageInfo = new PageInfo<SysRoleEntity>(sysRoleEntities);

		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}
		return Result.ok().put("page", pageInfo);
	}
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:role:select")
	public Result select(){

		//如果不是超级管理员，则只查询自己所拥有的角色列表
		SysRoleEntity sysRoleEntity = new SysRoleEntity();
		sysRoleEntity.setCreateBy(getUserId());
		List<SysRoleEntity> list;
		try {
			list = sysRoleService.select(sysRoleEntity);
		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}

		return Result.ok().put("list", list);
	}
	
	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public Result info(@PathVariable("roleId") Long roleId){

		SysRoleEntity role;
		try {
			role = sysRoleService.selectByPrimaryKey(roleId);
		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}

		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		
		return Result.ok().put("role", role);
	}
	
	/**
	 * 保存角色
	 */
	@SysLog("保存角色")
	@RequestMapping("/save")
	@RequiresPermissions("sys:role:save")
	public Result save(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		
		role.setCreateBy(getUserId());
		role.setUpdateBy(getUserId());
		try {
			List<SysRoleEntity> roles = sysRoleService.select(role);
			if (!CollectionUtils.isEmpty(roles)) {
				logger.error("角色已经存在{}",role);
				return Result.error("角色已经存在");
			}
			role.setCreateTime(new Date());
			sysRoleService.insertSelective(role);
		} catch (Exception e) {
			logger.error("保存数据失败！");
		}

		return Result.ok();
	}
	
	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@RequestMapping("/update")
	@RequiresPermissions("sys:role:update")
	public Result update(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		
		try {
			role.setUpdateBy(getUserId());
			Condition condition = new Condition(SysRoleEntity.class);
			condition.createCriteria().andEqualTo("updateTime",role.getUpdateTime())
					.andEqualTo("roleId",role.getRoleId());
			sysRoleService.updateByCondition(role,condition);
		} catch (Exception e) {
			logger.error("修改角色失败");
			return Result.error("修改角色失败");
		}
		return Result.ok();
	}
	
	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public Result delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		
		return Result.ok();
	}
}
